package com.homedo.odin.solution.service.common.impl;

import com.homedo.core.common.util.DateUtils;
import com.homedo.core.common.util.JsonUtils;
import com.homedo.odin.solution.config.PlatformConfigHelper;
import com.homedo.odin.solution.model.dto.*;
import com.homedo.odin.solution.service.common.HomedoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.*;

/**
 * @author: zhaoxudong
 * @date: 2018/3/28 15:23
 * @Description:
 */
@Service
public class HomedoServiceImpl implements HomedoService {

    private final static Logger logger = LoggerFactory.getLogger(HomedoServiceImpl.class);

    @Autowired
    PlatformConfigHelper platformConfigHelper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<PdfImagesResponseBean> getPdfToImages(String url) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(platformConfigHelper.getPdfGetimagesUrl())
                .queryParam("url", url);
        try {
            ResponseEntity<PdfImagesResponseBean[]> responseEntity = restTemplate.getForEntity(builder.toUriString(),
                    PdfImagesResponseBean[].class);
            logger.info(JsonUtils.toJSONString(responseEntity.getBody()));
            return Arrays.asList(responseEntity.getBody());
        } catch (Exception e) {
            logger.error("请求平台pdf转图片接口异常", e);
        }
        return Collections.emptyList();
    }

    @Override
    public PcAccountInfo getUserMessageInfo(Integer userId) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(platformConfigHelper.getGetUserMessageUrl())
                .queryParam("accountId", userId);
        try {
            ResponseEntity<PcAccountInfo> responseEntity = restTemplate
                    .getForEntity(builder.toUriString(), PcAccountInfo.class);
            logger.info(JsonUtils.toJSONString(responseEntity.getBody()));
            return responseEntity.getBody();
        } catch (Exception e) {
            logger.error("请求平台获取用户信息接口异常", e);
        }
        return null;
    }

    @Override
    public void sendShortMessage(ShortMessageBean shortMessageBean) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(platformConfigHelper.getShortMessageUrl())
                .queryParam("accountId", shortMessageBean.getAccountId())
                .queryParam("phone", shortMessageBean.getPhone())
                .queryParam("content", shortMessageBean.getContent());
        try {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(builder.toUriString(), String.class);
            logger.info(responseEntity.getBody());
        } catch (Exception e) {
            logger.error("请求平台发送短息接口异常", e);
        }
    }


    @Override
    public void addSolutionVipIntegral(Integer userId, Date createTime, Long solutionId) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(platformConfigHelper.getSolutionVipIntegral())
                .queryParam("accountId", userId)
                .queryParam("releaseDate", DateUtils.getDateString(createTime))
                .queryParam("solutionId", solutionId);
        try {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(builder.toUriString(), String.class);
            logger.info(responseEntity.getBody());
        } catch (Exception e) {
            logger.error("请求平台方案上传VIP积分接口异常", e);
        }
    }

    @Override
    public List<SkuListResponseBean> getSkusZB(String productIds) {
        List<SkuListResponseBean> list = new ArrayList<>();
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(platformConfigHelper.getProductsZBUrl())
                .queryParam("productIds", productIds)
                .queryParam("platform", "b2b");

        try {
            ResponseEntity<ProductListResponseBean[]> responseEntity = restTemplate.getForEntity(builder.toUriString(),
                    ProductListResponseBean[].class);
            logger.info(JsonUtils.toJSONString(responseEntity.getBody()));

            for (ProductListResponseBean productListResponseBean : responseEntity.getBody()) {
                SkuListResponseBean skuListResponseBean = new SkuListResponseBean();
                skuListResponseBean.setProductName(productListResponseBean.getProductName());
                skuListResponseBean.setCountSalesCount(productListResponseBean.getCountSalesCount());
                skuListResponseBean.setFileName(productListResponseBean.getFileName());
                skuListResponseBean.setPackUnit(productListResponseBean.getPackUnit());
                skuListResponseBean.setPrice(productListResponseBean.getPrice());
                skuListResponseBean.setProductId(productListResponseBean.getProductId());
                list.add(skuListResponseBean);
            }
            return list;
        } catch (Exception e) {
            logger.error("请求平台方案接口异常", e);
        }
        return Collections.emptyList();
    }

    @Override
    public List<ProductListResponseBean> getProductsZB(String productIds) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(platformConfigHelper.getProductsZBUrl())
                .queryParam("productIds", productIds)
                .queryParam("platform", "b2b");

        try {
            ResponseEntity<ProductListResponseBean[]> responseEntity = restTemplate.getForEntity(builder.toUriString(),
                    ProductListResponseBean[].class);
            logger.info(JsonUtils.toJSONString(responseEntity.getBody()));
            return Arrays.asList(responseEntity.getBody());
        } catch (Exception e) {
            logger.error("请求平台方案接口异常", e);
        }
        return Collections.emptyList();
    }
}
